<template>
  <div id="app">
    <router-view v-if="isRouterAlive"></router-view>
  </div>
</template>
<script>
import cookie from "./utils/cookie";
import aesUtils from "./plugins/aes-utils.js";
import Vue from "vue";
export default {
  name: "App",
  provide() {
    return {
      reload: this.reload,
    };
  },
  data() {
    return {
      accessToken: null,
      reload: this.reload,
      isRouterAlive: true,
      logoutTimer: null, // 定时器
      timeoutDuration: 30 * 60 * 1000, // 30分钟
    };
  },
  updated() {
    this.$store.commit(
      "setAccessToken",
      window.localStorage.getItem("access_token")
    );
  },
  watch: {
    "$store.state.accessToken"(val) {
      //上传接口统一添加自定义请求头
      let iv = this.$store.state.iv;
      let salt = this.$store.state.salt;
      let key = cookie.getCookie("randmId");
      let access_user;
      if (iv && salt && key) {
        access_user = aesUtils.decrypt(
          salt,
          iv,
          key,
          cookie.getCookie("access_user")
        );
      }
      if (this.$store.state.env != "dev") {
        Vue.prototype.fromData = {
          ContentType: "multipart/form-data",
          Authorization: "Bearer " + val,
          "OPERATE-USER": access_user,
          "FORM-ENCODE": 0,
        };
      }
    },
  },
  mounted() {
    this.startLogoutTimer();
    this.addEventListeners();
    // let top = window.localStorage.getItem("top")
    // if (!top) {
    // 	this.$router.push("/login")
    // }

    
  },
  methods: {
       // 设置定时器
       startLogoutTimer() {
        this.logoutTimer = setTimeout(() => {
          if (this.$route.name!=='login') {
          this.logout();           
          }
        }, this.timeoutDuration);
      },
      // 重置定时器
      resetLogoutTimer() {
          
        clearTimeout(this.logoutTimer);
        this.startLogoutTimer();
      },
      // 登出逻辑
      logout() {
        this.$Notice.success({
          title: '由于长时间无操作，您已自动退出登录。',
          duration: 0
        })
        // 调用你的登出 API 或者逻辑
        window.localStorage.removeItem("access_token");
            window.localStorage.removeItem("top");
            window.localStorage.removeItem("sun1");
            window.localStorage.removeItem("sun2");
            window.localStorage.removeItem("sun3");
            window.localStorage.removeItem("sun4");
            window.localStorage.removeItem("sun5");
            // window.localStorage.clear();
            // window.sessionStorage.clear();
            cookie.delCookie("userName");
            cookie.delCookie("userIcon");
            cookie.delCookie("randmId");
            cookie.delCookie("operateUserId");
            cookie.delCookie("idtt");
            cookie.delCookie("idttC");
            cookie.delCookie("access_user");
            cookie.delCookie("ownArea");
            // 公用方法
            // this.functionJS.queryNavgationTo(this, '/login');
            window.location.href = "#/login";
            window.close();
      },
      // 监听用户操作事件
      addEventListeners() {
        window.addEventListener("mousemove", this.resetLogoutTimer);
        window.addEventListener("keypress", this.resetLogoutTimer);
        window.addEventListener("click", this.resetLogoutTimer);
        window.addEventListener("scroll", this.resetLogoutTimer);
      },
      // 移除事件监听器
      removeEventListeners() {
        window.removeEventListener("mousemove", this.resetLogoutTimer);
        window.removeEventListener("keypress", this.resetLogoutTimer);
        window.removeEventListener("click", this.resetLogoutTimer);
        window.removeEventListener("scroll", this.resetLogoutTimer);
      },
    reload() {
      this.isRouterAlive = false;
      this.$nextTick(function () {
        this.isRouterAlive = true;
      });
    },
  },
  components: {},
  updated() {
    let imgs = [].slice.call(document.getElementsByTagName("img"), 0);
    imgs.forEach((el) => {
      el.onmousedown = (e) => {
        e.preventDefault();
        return false;
      };
    });
  },
};
</script>
<style lang="less" scoped>
</style>
